
<template>
	<view>
		<view style="padding-bottom:50px">
			<view v-if="length == 1" >
				<view v-for="(item,index) in res" :key="index" class="webkit" style="padding: 26upx 24upx;background: #fff;margin-bottom: 20upx;">
					<view>
						<checkbox-group  :data-id="item.id" v-if="edit">
							<label class="checkbox">
								<checkbox :value="item.id" style="margin-top: 68upx;"/>
							</label>
						</checkbox-group>
						<checkbox-group @change="select" :data-id="item.id" v-else>
							<label class="checkbox">
								<checkbox :value="item.id" :checked= "item.selected" style="margin-top: 68upx;"/>
							</label>
						</checkbox-group>
					</view>
					<image :src="item.thumb" mode="" style="width: 192upx;height:192upx;margin-right: 20upx;" @click="jump" :data-id="item.goodsid"></image>
					<view class="flex">
						<view style="line-height: 40upx;height: 80upx;" @click="jump" :data-id="item.goodsid" class="ellipsis-2" v-cloak>{{item.title}}</view>
						<view class="webkit" style="margin-bottom: 10upx;">
							<text class="flex" style="text-decoration: line-through;color: #999;" v-cloak>¥{{item.productprice}}</text>
							<!-- <image src="../../static/jifen.png" mode="" style="width: 36upx;height: 36upx;margin-top: 6upx;margin-right: 4upx;"></image> -->
						</view>
						<view class="webkit">
							<view class="flex" style="color: #f55;line-height: 52upx;font-size: 30upx;" v-cloak>¥{{item.marketprice}}</view>
							<view>
								<uni-number-box :min="item.minbuy" :max="item.maxbuy" :value="item.total" v-on:change="update($event,item.id)"></uni-number-box>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view v-else style="padding-top: 120upx;">
				<image src="../../../static/nogoods.png" style="margin-bottom:20upx;width:240upx;height:240upx;margin:0 auto;display:block;"></image>
				<view style="text-align:center;color:#999;font-size:30upx;margin-top:20upx;">您的购物车中没有商品哦！</view>
				<button style="border-radius: 40px;height: 40px;line-height: 40px;width: 140px;font-size: 14px;color: #EB574B;border: 1px solid #EB574B;margin-top: 20px;box-sizing: content-box;padding: 0;" @click="goIndex">去首页逛逛吧</button>
			</view>
			<view v-if="length == 1" >
			</view>
		</view>
		<view class="webkit" style="position: fixed;bottom: 0;width:100%;height: 50px;border-top: 1px solid #f5f5f5;background: #fff;box-sizing: border-box;z-index: 999;">
			<checkbox-group class="flex" style="width: 68px;margin-top: 10px;margin-left: 12px;" @change="checkall">
				<label class="checkbox">
					<checkbox value="" :checked= "allchecked" :disabled="length != 1?true:false"/>
					<text style="position: relative;top: 1px;color: #666;">全选</text>
				</label>
			</checkbox-group>
			<button type="warn" style="height: 49px;width: 100px;font-size: 13px;line-height: 49px;border-radius: 0;margin: 0;background: #ffa800;" :disabled="!subDisable" @click="fav">移到关注</button>
			<button type="warn" style="height: 49px;width: 100px;font-size: 13px;line-height: 49px;border-radius: 0;margin: 0;" :disabled="!subDisable" @click="del">删除</button>
		</view>
	</view>
</template>

<script>
	import uniNumberBox from '../../../components/uni-number-box.vue'
	export default {
		components: {
			uniNumberBox
		},
		data() {
			return {
				show_hide:'block',
				res:[],
				bianji:0,
				list:[],
				listbox:[],
				alltotal:0,
				num:0,
				allchecked:false,
				edit:false,
				openid:'',
				subDisable: false,
				length:1,
				ids:[]
			};
		},
		onLoad:function(){
			uni.showLoading()
			uni.getStorage({
				key: 'openid',
				success: (res) => {
					this.openid = res.data
					this.setAjax()
				},
				fail: () => {
					uni.navigateTo({
						url: '../login/login'
					});
				}
			});
		},
		methods:{
			setAjax(){
				uni.request({
					url: this.$BASE_URL+'/index.php?i=2&c=entry&m=ewei_shopv2&do=mobile&r=member.cart.get_list&app=1',
					method: 'POST',
					data: {'openid':this.openid},
					header:{'Content-Type':'application/x-www-form-urlencoded'},
					success: res => {
						uni.hideLoading()
						this.res = res.data.result.list
						console.log(res.data.result.list)
						if(res.data.result.list.length == 0){
							this.length = 0
						}
						var all = 0,check= 0,allnum = 0
						for(var i in this.res){
							if(this.res[i].selected == 0){
								this.res[i].selected = false
							}else{
								this.res[i].selected = true
							}
							var totalmaxbuy = Number(this.res[i].totalmaxbuy)
							var minbuy = Number(this.res[i].minbuy)
							var total = Number(this.res[i].total)
							if(this.res[i].maxbuy == 0){
								this.res[i].maxbuy = totalmaxbuy
							}
							this.res[i].minbuy = minbuy
							if(total < minbuy){
								this.res[i].total = minbuy
							}else{
								this.res[i].total = total
							}
							var ary = {id:"",total:"",optionid:"",checked:false}
							ary.id = this.res[i].id
							ary.total = this.res[i].total
							ary.optionid = this.res[i].optionid
							ary.checked = this.res[i].selected
							this.list.push(ary)
							
							if(this.res[i].selected == true){
								var price = this.res[i].marketprice *this.res[i].total
								all = all + price
								allnum = allnum + this.res[i].total
							}else{
								check = check+1
							}
						}
						if(check == 0){
							this.allchecked = true
						}else{
							this.allchecked = false
						}
						if(check == this.res.length){
							this.subDisable = false
						}else{
							this.subDisable = true
						}
						var n = this.splitThousands(all,2)
						this.alltotal = this.getFloatStr(n)
						this.num = allnum
					},
					fail: () => {},
					complete: () => {}
				});
			},
			getFloatStr(num){  
				num += '';  
				num = num.replace(/[^0-9|\.]/g, ''); //清除字符串中的非数字非.字符
				if(/^0+/) //清除字符串开头的0  
					num = num.replace(/^0+/, '');  
				if(!/\./.test(num)) //为整数字符串在末尾添加.00  
					num += '.00';  
				if(/^\./.test(num)) //字符以.开头时,在开头添加0  
					num = '0' + num;  
				num += '00';        //在字符串末尾补零  
				num = num.match(/\d+\.\d{2}/)[0];  
				return num
			},
			jump(e){
				uni.navigateTo({
					url: '../goods/detail?goodsid='+e.currentTarget.dataset.id
				});
			},
			goIndex(){
				uni.switchTab({
					url:'../../home/home'
				})
			},
			select(e){
				uni.showLoading()
				var select = 1
				if(e.detail.value == ""){
					select = 0
				}
				uni.request({
					url: this.$BASE_URL+'/index.php?i=2&c=entry&m=ewei_shopv2&do=mobile&r=member.cart.select&app=1',
					method: 'POST',
					data: {
						'openid':this.openid,
						'id':e.currentTarget.dataset.id,
						'select':select,
						},
				header:{'Content-Type':'application/x-www-form-urlencoded'},
					success: res => {
						this.setAjax()
					},
					fail: () => {},
					complete: () => {}
				});
			},
			splitThousands(num, fixed) {
				if (typeof num !== "number") {num = parseFloat(num);}
				var reg = /\B(?=(\d{3})+$)/g;
				num = num.toString().split(".");
				fixed = fixed == undefined ? 2 : fixed;
				num[0] = num[0].replace(reg, ",");
				num[1] = num[1] ? num[1].substr(0, fixed) : "00000000000000000".substr(0, fixed);
				return fixed ? num.join(".") : num[0];
			},
			update(value,id){
				uni.request({
					url: this.$BASE_URL+'/index.php?i=2&c=entry&m=ewei_shopv2&do=mobile&r=member.cart.select&app=1',
					method: 'POST',
					data: {'openid':this.openid,'id':id,'select':1,},
					header:{'Content-Type':'application/x-www-form-urlencoded'}
				});
				for(var i in this.list){
					if(this.list[i].id == id){
						this.list[i].total = value
						uni.showLoading()
						uni.request({
							url: this.$BASE_URL+'/index.php?i=2&c=entry&m=ewei_shopv2&do=mobile&r=member.cart.update&app=1',
							method: 'POST',
							data: {
								'openid':this.openid,
								'id':id,
								'optionid':this.list[i].optionid,
								'total':value
								},
							header:{'Content-Type':'application/x-www-form-urlencoded'},
							success: res => {
								this.setAjax()
							}
						});
					}
				}
				
			},
			checkall(e){
				uni.showLoading()
				for(var i in this.list){
					if(this.allchecked == false){
						if(this.list[i].checked == false){
							uni.request({
								url: this.$BASE_URL+'/index.php?i=2&c=entry&m=ewei_shopv2&do=mobile&r=member.cart.select&app=1',
								method: 'POST',
								data: {'openid':this.openid,'id':this.list[i].id,'select':1,},
								header:{'Content-Type':'application/x-www-form-urlencoded'},
								success: res => {
									this.setAjax()
								}
							});
						}
					}else{
						uni.request({
							url: this.$BASE_URL+'/index.php?i=2&c=entry&m=ewei_shopv2&do=mobile&r=member.cart.select&app=1',
							method: 'POST',
							data: {'openid':this.openid,'id':this.list[i].id,'select':0,},
							header:{'Content-Type':'application/x-www-form-urlencoded'},
							success: res => {
								this.setAjax()
							}
						});
					}
				}
			},
			del(){
				uni.showModal({
					content: "确认要从购物车删除吗?",
					confirmText: "确定",
					cancelText: "取消",
					success: res => {
						if(res.confirm){
							var d1 = '{"openid":"'+this.openid+'"'
							var index = 0
							for(var i in this.res){
								if(this.res[i].selected == true){
									d1 += ',"ids['+index+']":"'+this.res[i].id+'"'
									index++
								}
							}
							var data = d1+'}'
							data = JSON.parse(data)
							uni.request({
								url: this.$BASE_URL+'/index.php?i=2&c=entry&m=ewei_shopv2&do=mobile&r=member.cart.remove&app=1',
								method: 'POST',
								data: data,
								header:{'Content-Type':'application/x-www-form-urlencoded'},
								success: res => {
									uni.showToast({
										title: '删除成功',
										icon:"none"
									});
									this.setAjax()
								}
							});
							
						}
					}
				})
			},
			fav(){
				uni.showModal({
					content: "确认要从购物车移到关注吗?",
					confirmText: "确定",
					cancelText: "取消",
					success: res => {
						if(res.confirm){
							var d1 = '{"openid":"'+this.openid+'"'
							var index = 0
							for(var i in this.res){
								if(this.res[i].selected == true){
									d1 += ',"ids['+index+']":"'+this.res[i].id+'"'
									index++
								}
							}
							var data = d1+'}'
							data = JSON.parse(data)
							uni.request({
								url: this.$BASE_URL+'/index.php?i=2&c=entry&m=ewei_shopv2&do=mobile&r=member.cart.tofavorite&app=1',
								method: 'POST',
								data: data,
								header:{'Content-Type':'application/x-www-form-urlencoded'},
								success: res => {
									uni.showToast({
										title: '移到关注成功',
										icon:"none"
									});
									this.setAjax()
								}
							});
							
						}
					}
				})
			}
		}
	}
</script>

<style>button:after{display:none;}</style>